// =============================================================
// CMessage
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Sep. 4, 2007
// =============================================================
#include "CMessage.h"

CMessage::CMessage() 
{ 
	message = NULL; 
}

CMessage::CMessage(const CMessage &obj) 
{
	message = new char[strlen((char *)obj.message)+1];
	strcpy((char *)message, (char *)obj.message);
}

CMessage::~CMessage() 
{
	if((char *)message!=NULL) delete []message;
}

bool CMessage::blindlyEquals(const CObject *rhs) const 
{
#ifdef DEBUG
cout << "[CMessage] blindlyEquals" << endl;
#endif
	const CMessage *casted_rhs = dynamic_cast<const CMessage *>(rhs);
	// perform comparisons on private message
	bool isEqual = (strcmp(this->message, casted_rhs->message)==0);
	return (isEqual&&CObject::blindlyEquals(rhs));
}

const type_info *CMessage::getTypeEquiv() const 
{
#ifdef DEBUG
cout << "[CMessage] typeid name = " << typeid(this).name() << endl;
#endif

	return &typeid(this);
}


CMessage *CMessage::clone() const 
{
	return new CMessage(*this);
}

CMessage &CMessage::operator=(CMessage &obj) 
{
	// identity test
	if(this==&obj) return *this;

	message = new char[strlen((char *)obj.message)+1];
	strcpy((char *)message, (char *)obj.message);

	return *this;
}

int CMessage::set(char *msg) 
{
	if((char *)message!=NULL) {
		delete []message;
		message = NULL;
	}

	message = new char[strlen(msg)+1];
	strcpy((char *)message, (char *)msg);

	return strlen((char *)message);
}

char *CMessage::get() 
{ 
	return (char *) message; 
}

void CMessage::print() 
{ 
	cout << message << " on (ttl = " << getTTL() << ")" << endl; 
}
